﻿using ERPSystem.Common.Domains.PurchaseInvoiceCommon;
using ERPSystem.Common.DTO;
using ERPSystem.Common.Infrastructure.IRepository;
using ERPSystem.Enum.CommonErrorCodes;
using MediatR;
using NPOI.Util;
using SqlSugar;

namespace SalesManagementCommon.API.SalesManagementCommons.PurchaseReturnOrderRead
{
    public class PurchaseReturnOrderQueryHandler : IRequestHandler<PurchaseReturnOrderQueryCommand, Result<PageResultDto<PurchaseReturnOrderQueryDto>>>
    {
        private readonly ISqlSugarClient _sqlSugarClient;

        public PurchaseReturnOrderQueryHandler(ISqlSugarClient sqlSugarClient)
        {
            _sqlSugarClient = sqlSugarClient;
        }
        /// <summary>
        /// 获取采购退货单
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result<PageResultDto<PurchaseReturnOrderQueryDto>>> Handle(PurchaseReturnOrderQueryCommand request, CancellationToken cancellationToken)
        {
            var result = new Result<PageResultDto<PurchaseReturnOrderQueryDto>>();
            var page = new PageResultDto<PurchaseReturnOrderQueryDto>() { TotalCount = 0, TotalPage = 0, List = new List<PurchaseReturnOrderQueryDto>() };
            try
            {
                RefAsync<int> totalCount = 0;
                RefAsync<int> totalPage = 0;
                var purchaseReturnOrderQueryList = _sqlSugarClient.Queryable<PurchaseReturnOrder, PurchaseReturnOrderSubtable>((a, b) => a.PurchaseReturnOrderSerialNumber == b.PurchaseReturnOrderSerialNumber).Select((a, b) => new PurchaseReturnOrderQueryDto()
                {
                    PurchaseReturnOrderId = a.PurchaseReturnOrderId,
                    CopyNo = b.CopyNo,
                    PurchaseReturnOrderNo = a.PurchaseReturnOrderNo,
                    DocumentsStatus = a.DocumentsStatus,
                    Supplier = a.Supplier,
                    MaterialName = b.MaterialName,
                    MaterialNo = b.MaterialNo,
                    OutboundShipmentsNum = b.OutboundShipmentsNum,
                    DocumentDate = a.DocumentDate
                });
                if (!string.IsNullOrEmpty(request.CopyNo))
                {
                    purchaseReturnOrderQueryList = purchaseReturnOrderQueryList.Where(c => c.CopyNo.Contains(request.CopyNo));
                }
                if (!string.IsNullOrEmpty(request.PurchaseReturnOrderNo))
                {
                    purchaseReturnOrderQueryList = purchaseReturnOrderQueryList.Where(c => c.PurchaseReturnOrderNo.Contains(request.PurchaseReturnOrderNo));
                }
                if (!string.IsNullOrEmpty(request.MaterialNo))
                {
                    purchaseReturnOrderQueryList = purchaseReturnOrderQueryList.Where(c => c.MaterialNo.Contains(request.MaterialNo));
                }
                if (!string.IsNullOrEmpty(request.MaterialName))
                {
                    purchaseReturnOrderQueryList = purchaseReturnOrderQueryList.Where(c => c.MaterialName.Contains(request.MaterialName));
                }
                if (request.StartDocumentDate != null)
                {
                    purchaseReturnOrderQueryList = purchaseReturnOrderQueryList.Where(c => c.DocumentDate >= request.StartDocumentDate);
                }
                if (request.EndDocumentDate != null)
                {
                    purchaseReturnOrderQueryList = purchaseReturnOrderQueryList.Where(c => c.DocumentDate <= request.EndDocumentDate);
                }
                var res = await purchaseReturnOrderQueryList.ToPageListAsync(request.PageIndex, request.PageSize, totalCount, totalPage);
                page.TotalCount = totalCount;
                page.TotalPage = totalPage;
                page.List = res;
                if (page.List.Any())
                {
                    result.Code = (int)CommonErrorCode.Success;
                    result.Data = page;
                    result.Msg = "获取采购退货单成功";
                }
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
